/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import dk.lindhardt.gwt.geie.shared.Formula;
import dk.lindhardt.gwt.geie.shared.Position;

import java.util.HashMap;
import java.util.Map;

/**
 * User: AnAmuser
 * Date: 31-08-11
 * <p/>
 * Converts formulas from display string to geie formulas an visa versa
 */
public class FormulaConverter {

   private final static RegExp positionPattern = RegExp.compile("\\[\\s*(\\d*)\\,\\s*(\\d*)\\]", "g");

   public static Formula convertString(String displayString) {
      String formulaString = "";
      String[] splitResult = displayString.split(positionPattern.getSource());
      MatchResult matchResult;
      Map<Position, String> usedVariables = new HashMap<Position, String>();
      int index = 0;
      while ((matchResult = positionPattern.exec(displayString)) != null) {
         if (splitResult.length > 0) {
            formulaString += splitResult[index];
         }
         int row = Integer.parseInt(matchResult.getGroup(1));
         int column = Integer.parseInt(matchResult.getGroup(2));
         Position position = new Position(row, column);
         if (usedVariables.containsKey(position)) {
            formulaString += usedVariables.get(position);
         } else {
            String variableName = "x" + usedVariables.size();
            usedVariables.put(position, variableName);
            formulaString += variableName;
         }
         index++;
      }

      int lastSplitResultIndex = splitResult.length - 1;
      if (index <= lastSplitResultIndex) {
         formulaString += splitResult[lastSplitResultIndex];
      }

      Formula formula = new Formula(formulaString);

      formula.setCellVariables(inverseMap(usedVariables));
      return formula;
   }

   private static Map<String, Position> inverseMap(Map<Position, String> map) {
      Map<String, Position> result = new HashMap<String, Position>();
      for (Position position : map.keySet()) {
         String string = map.get(position);
         result.put(string, position);
      }
      return result;
   }

   public static String convertFormula(Formula formula) {
      String formulaString = formula.getFormula();
      Map<String, Position> cellVariables = formula.getCellVariables();
      for (String variable : cellVariables.keySet()) {
         Position position = cellVariables.get(variable);
         formulaString = formulaString.replaceAll(
               variable, "[" + position.getRow() + "," + position.getColumn() + "]");
      }
      return formulaString;
   }
}
